from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QStyle

from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QSystemTrayIcon, QMenu, QAction


class SystemTray(QSystemTrayIcon):
    def __init__(self, main_window, parent=None):
        super().__init__(parent)
        self.main_window = main_window
        self.init_ui()
        self.init_menu()
        self.activated.connect(self.click_event)

    def init_ui(self):
        app_icon = QApplication.style().standardIcon(QStyle.SP_ComputerIcon)
        # self.setIcon(QIcon("1.png"))
        self.setIcon(app_icon)
        self.show()

    def init_menu(self):
        self.menu = QMenu()
        # self.showAction1 = QAction("启动", self, triggered=self.show_window)
        # self.showAction2 = QAction("显示通知", self, triggered=self.showMsg)
        self.quitAction = QAction("退出", self, triggered=self.quit)

        # self.menu.addAction(self.showAction1)
        # self.menu.addAction(self.showAction2)
        self.menu.addAction(self.quitAction)
        self.setContextMenu(self.menu)

        # 设置图标

        # 把鼠标点击图标的信号和槽连接

    def showMsg(self):
        pass

    #     self.showMessage("Message", "skr at here", self.icon)

    def show_window(self):
        pass

    #     # 若是最小化，则先正常显示窗口，再变为活动窗口（暂时显示在最前面）
    #     self.ui.showNormal()
    #     self.ui.activateWindow()

    def quit(self):
        self.main_window.close()
        QtWidgets.qApp.quit()

    # # 鼠标点击icon传递的信号会带有一个整形的值，1是表示单击右键，2是双击，3是单击左键，4是用鼠标中键点击
    def click_event(self, reason):
        if reason == 2:
            if self.main_window.isHidden():
                self.main_window.show()
                self.main_window.activateWindow()
            else:
                self.main_window.activateWindow()  # 如果已显示则激活窗口
            # self.showMessage("Message", "skr at here", self.icon)
            # if self.ui.isMinimized() or not self.ui.isVisible():
            #     # 若是最小化，则先正常显示窗口，再变为活动窗口（暂时显示在最前面）
            #     self.ui.showNormal()
            #     self.ui.activateWindow()
            #     self.ui.setWindowFlags(QtCore.Qt.Window)
            #     self.ui.show()
            # else:
            #     # 若不是最小化，则最小化
            #     self.ui.showMinimized()
            #     self.ui.setWindowFlags(QtCore.Qt.SplashScreen)
            #     self.ui.show()
            #     # self.ui.show()




# from PyQt5 import QtWidgets
# from PyQt5.QtWidgets import QApplication, QStyle
# from PyQt5.QtGui import QIcon
# from PyQt5.QtWidgets import QSystemTrayIcon, QMenu, QAction

# class SystemTray(QSystemTrayIcon):
#     def __init__(self, main_window, parent=None):
#         super().__init__(parent)
#         self.main_window = main_window
#         self.toggle_action = None  # 新增：保存切换动作的引用
#         self.init_ui()
#         self.init_menu()
#         self.activated.connect(self.click_event)
        
#         # 监听窗口状态变化
#         self.main_window.installEventFilter(self)

#     def init_ui(self):
#         app_icon = QApplication.style().standardIcon(QStyle.SP_ComputerIcon)
#         self.setIcon(app_icon)
#         self.show()

#     def init_menu(self):
#         self.menu = QMenu()
        
#         # 动态切换动作
#         self.toggle_action = QAction("显示", self)
#         self.toggle_action.triggered.connect(self.toggle_window)
#         self.update_toggle_action_text()  # 初始化文本
        
#         self.quitAction = QAction("退出", self, triggered=self.quit)
        
#         self.menu.addAction(self.toggle_action)
#         self.menu.addAction(self.quitAction)
#         self.setContextMenu(self.menu)

#     def toggle_window(self):
#         if self.main_window.isHidden():
#             self.main_window.show()
#             self.main_window.activateWindow()
#         else:
#             self.main_window.hide()
#         self.update_toggle_action_text()

#     def update_toggle_action_text(self):
#         """根据窗口状态更新菜单项文本"""
#         if self.main_window.isHidden():
#             self.toggle_action.setText("显示")
#         else:
#             self.toggle_action.setText("隐藏")

#     def eventFilter(self, obj, event):
#         """监听窗口状态变化事件"""
#         if obj == self.main_window:
#             if event.type() in (QtCore.QEvent.Hide, QtCore.QEvent.Show):
#                 self.update_toggle_action_text()
#         return super().eventFilter(obj, event)

#     def quit(self):
#         self.main_window.close()
#         QtWidgets.qApp.quit()

#     def click_event(self, reason):
#         if reason == 2:  # 双击
#             self.toggle_window()
